package com.hcss.sys.role.controller;

import com.hcss.paging.constant.PagingConstant;
import com.hcss.sys.role.constant.RoleConstants;
import com.hcss.sys.role.model.HcSysRole;
import com.hcss.sys.role.model.HcSysRoleuser;
import com.hcss.sys.role.model.par.HcSysRolePar;
import com.hcss.sys.role.model.vo.HcSysRolemenuVO;
import com.hcss.sys.role.service.RoleService;
import com.hcss.update.model.UpdateSchema;
import com.hcss.util.AjaxResult;
import com.hcss.util.BeanCopyUtil;
import com.hcss.util.EmptyUtil;
import com.hcss.util.ListUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
import java.util.Map;

/**
 * @描述: Role功能模块
 * @作者: fangtao
 * @创建日期: 2019-01-08 10:46:19
 */
@Slf4j
@RestController
@RequestMapping("role")
public class RoleController {

	@Autowired
    private RoleService roleService;


	/**
	 * 更新插入的schema，用于鉴定数据的安全性
	 */
    static UpdateSchema updateSchema = new UpdateSchema(HcSysRole.class, new String[]{"name", "code"}, null);

	/**
	 * Role数据列表页面
     *
	 * @return
	 */
	@GetMapping("listPage")
	public ModelAndView listPage(){
		ModelAndView mav = new ModelAndView("/role/role-listPage");
        return mav;
	}
	
	/**
     * Role分页数据接口
     *
     * @return
     */
	@PostMapping("/all/valid")
	public AjaxResult allValid(){
        try {
			List<HcSysRole> list = roleService.selectAll();
			return new AjaxResult(list);
        } catch (Exception e) {
            log.error("数据查询出错", e);
            return AjaxResult.getErrorResult(e.getMessage());
        }
	}

	/**
	 * Role新增或修改数据接口
	 *
	 * @param par 数据对象
	 * @return
	 */
	@PostMapping("/addOrUpdate")
	public AjaxResult addOrUpdate(HcSysRolePar par){
		if (!UpdateSchema.checkAddOrUpdate(par, updateSchema)) {
            return AjaxResult.getMissingParamResult();
        }
		// 校验code唯一性
		if (!roleService.checkCodeUnique(par.getCode(),par.getId())) {
			return AjaxResult.getErrorResult("CODE[" + par.getCode() + "]已存在");
		}
		try {
			HcSysRole model = BeanCopyUtil.copyConvey(par, HcSysRole.class);
			model.setStatus(RoleConstants.STATUS_NORMAL);
			if (EmptyUtil.isHaveEmpty(model.getId())) {
				roleService.insertModel(model);
			}else{
				roleService.updateModel(model);
			}
			return new AjaxResult(model.getId());
		} catch (Exception e) {
			log.error("新增或更新操作出错，参数：" + par.toString(), e);
			return AjaxResult.getErrorResult(e.getMessage());
		}
	}
    
    /**
	 * 根据id进行删除
	 * @param model
	 * @return
	 */
	@PostMapping("/delete")
	public AjaxResult deleteByIds(HcSysRole model){
		if (EmptyUtil.isHaveEmpty(model.getId())) {
            return AjaxResult.getMissingParamResult();
        }
		try {
			roleService.deleteById(model);
			return new AjaxResult();
		} catch (Exception e) {
            log.error("批量删除出错，参数：[{}]",model, e);
            return AjaxResult.getErrorResult(e.getMessage());
		}
	}

	/**
	 * 根据菜单id获取关联的角色集合
	 * @param rolemenuVO
	 * @return
	 */
	@PostMapping("/getByMenu")
	public AjaxResult getByMenu(HcSysRolemenuVO rolemenuVO) {
		if (EmptyUtil.isHaveEmpty(rolemenuVO.getMenuid())) {
            return AjaxResult.getMissingParamResult();
        }

		List<HcSysRole> selectedRole = roleService.selectByMenu(rolemenuVO);
		return new AjaxResult(selectedRole);
	}

	/**
	 * 保存角色菜单信息
	 * @param model
	 * @return
	 */
	@PostMapping("/menu/save")
	public AjaxResult saveRoleMenu(HcSysRolemenuVO model) {
		if (EmptyUtil.isHaveEmpty(model.getMenuid(), model.getRoleid())) {
            return AjaxResult.getMissingParamResult();
        }
		roleService.saveRoleMenu(model);
		return new AjaxResult();
	}

	/**
	 * 删除角色菜单信息
	 * @param model
	 * @return
	 */
	@PostMapping("/menu/delete")
	public AjaxResult deleteRoleMenu(HcSysRolemenuVO model) {
		if (EmptyUtil.isHaveEmpty(model.getMenuid(), model.getRoleid())) {
            return AjaxResult.getMissingParamResult();
        }
		roleService.deleteRoleMenu(model);
		return new AjaxResult();
	}

	/**
	 * 批量保存角色菜单
	 * @param roleid
	 * @param menuids
	 * @return
	 */
	@PostMapping("/menu/save/batch")
	public AjaxResult batchSaveRoleMenu(String roleid, String menuids) {
		if (EmptyUtil.isHaveEmpty(roleid, menuids)) {
            return AjaxResult.getMissingParamResult();
        }
		try {
			List<String> menuidList = ListUtil.convey(ListUtil.split(menuids, PagingConstant.defaultSplit), String.class);
			roleService.batchSaveRoleMenu(roleid, menuidList);
			return new AjaxResult();
		} catch (Exception e) {
			log.error("批量保存角色菜单出错，参数：roleid=" + roleid + ", menuids=" + menuids, e);
			return AjaxResult.getErrorResult(e.getMessage());
		}
	}

	/**
	 * 解除角色的全部绑定菜单
	 * @param roleid
	 * @return
	 */
	@PostMapping("/menu/delete/all")
	public AjaxResult deleteAllRoleMenu(String roleid) {
		if (EmptyUtil.isHaveEmpty(roleid)) {
            return AjaxResult.getMissingParamResult();
        }
		roleService.deleteRoleMenuByRoleid(roleid);
		return new AjaxResult();
	}

	/**
	 * 根据角色id获取关联的用户集合
	 * @param roleid
	 * @return
	 */
	@PostMapping("/user/list")
	public AjaxResult getUserByRole(String roleid) {
		if (EmptyUtil.isHaveEmpty(roleid)) {
            return AjaxResult.getMissingParamResult();
        }

		List<Map<String, String>> userList = roleService.selectUserByRole(roleid);
		return new AjaxResult(userList);
	}

	/**
	 * 保存角色用户信息
	 * @param model
	 * @return
	 */
	@PostMapping("/user/save")
	public AjaxResult saveRoleUser(HcSysRoleuser model) {
		if (EmptyUtil.isHaveEmpty(model.getUserid(), model.getRoleid())) {
            return AjaxResult.getMissingParamResult();
        }
		roleService.saveRoleUser(model);
		return new AjaxResult();
	}

	/**
	 * 删除角色用户信息
	 * @param model
	 * @return
	 */
	@PostMapping("/user/delete")
	public AjaxResult deleteRoleUser(HcSysRoleuser model) {
		if (EmptyUtil.isHaveEmpty(model.getUserid(), model.getRoleid())) {
            return AjaxResult.getMissingParamResult();
        }
		roleService.deleteRoleUser(model);
		return new AjaxResult();
	}
}